# -*- coding: utf-8 -*-
"""
Created on 2024-03-20 17:31:24
---------
@summary:
---------
@author: 11450
"""
import time

import feapder
from random import randint
from feapder.utils.webdriver import WebDriver
from selenium.webdriver.common.by import By


class Tourism(feapder.AirSpider):

    def download_midware(self, request):
        request.headers = {
            'Referer': 'https://www.mafengwo.cn/i/5376978.html',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'
        }

        return request

    def start_requests(self):
        yield feapder.Request("https://www.mafengwo.cn/gonglve/", page=1, render=True)

    def parse(self, request, response):
        browser: WebDriver = response.browser
        time.sleep(2)
        # 页面下滑
        self.drop_down(browser)
        div_list = browser.find_elements(
            By.XPATH,
            '//div[@class="_j_feed_data"]/div[@class="feed-item _j_feed_item"]'
        )
        for div in div_list:
            item = dict()
            item['star'] = div.find_element(By.XPATH,
                                            './/a/div[@class="bar clearfix"]/span/span').text
            item['title'] = div.find_element(By.XPATH, './/a/div[@class="title"]').text
            item['type'] = div.find_element(By.XPATH, './/a//span[@class="type"]/strong').text
            try:
                if item['type'] == '游记' or item['type'] == '官方账号':
                    item['content'] = div.find_element(By.XPATH, './/a//div[@class="info"]').text
                    item['author'] = div.find_element(By.XPATH, './/a//div[@class="ext-r"]/span').text
                    item['nums'] = div.find_element(By.XPATH, './/a//div[@class="ext-r"]/span[last()]').text
                else:
                    item['content'] = div.find_element(By.XPATH, './/a/div/div[@class="info"]').text

                    item['nums'] = div.find_element(By.XPATH, './/a/div[@class="imgs"]//li[@class="ext-r"]').text
            except Exception:
                print("错误")

            print(item)

        # 翻页
        request.page += 1
        if request.page < 10:
            # self.next_page(browser)
            # next_url = browser.current_url  # 获取翻页后的页面网址
            yield feapder.Request("https://www.mafengwo.cn/gonglve/", data={"page": request.page}, page=request.page,
                                  render=True,
                                  callback=self.parse)

    def drop_down(self, browser):
        for i in range(1, 3):
            js_code = f'document.documentElement.scrollTop = {i * 1000}'
            browser.execute_script(js_code)
            time.sleep(randint(1, 2))

    def next_page(self, browser):
        try:
            next_button = browser.find_element(By.XPATH, '//*[@class="_j_page_loader"]')
            if next_button:
                next_button.click()
            else:
                browser.close()
        except Exception as e:
            print('最后一页: ', e)
            browser.quit()


if __name__ == "__main__":
    Tourism().start()
